#include "jsontranslator.h"
#include <QFile>
#include <QDebug>
#include "UIEditor/uilib.h"
JsonTranslator::JsonTranslator()
{

}

JsonTranslator::~JsonTranslator()
{

}

QJsonObject JsonTranslator::ReadAndParseJson(QString filePath)
{
    QFile file(filePath);
    // 打开文件
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "Open file failed.";
    }
    // 读取文件内容
    QTextStream in(&file);
    // 设置编码方式
    in.setCodec("UTF-8");
    QString jsonStr = in.readAll();
    // 关闭文件
    file.close();
    // 将JSON字符串转换为QJsonObject对象
    QJsonDocument doc = QJsonDocument::fromJson(jsonStr.toUtf8());
    QJsonObject jsonObj = doc.object();
    return jsonObj;
}

QJsonObject *JsonTranslator::ToJson(NarrationNode *node)
{
    if(!node) return nullptr;

    QJsonObject* ob = new QJsonObject();

    QJsonObject* bgmob = ToJson(node->bgm);
    if(bgmob) ob->insert("bgm",*bgmob);

    QJsonObject* bgseob = ToJson(node->bgse);
    if(bgseob) ob->insert("bgse",*bgseob);

    ob->insert("bgmStop",node->stopBgm);
    ob->insert("bgseStop",node->stopBgse);

    ob->insert("soundPath",node->soundPath);
    ob->insert("name",node->name);
    ob->insert("text",node->text);
    ob->insert("id",node->id);
    ob->insert("nodeName",node->nodeName);

    ob->insert("childId",node->childId);
    ob->insert("parentId",node->parentId);
    QJsonArray imgs;
    for(int i = 0;i < node->image.count();i++)
    {
        imgs.append(*ToJson(node->image.at(i)));
    }
    ob->insert("imgs",imgs);

    //linkers
    QJsonArray linkers;
    for(int i = 0;i < node->linkers.count();i++)
    {
        linkers.append(*ToJson(node->linkers.at(i)));
    }
    ob->insert("linkers",linkers);

    //effect
    ob->insert("effect",node->effect);
    ob->insert("effectName",node->effectName);
    //black
    ob->insert("blackTime",node->blackTime);
    ob->insert("black",node->black);
    return ob;
}

QJsonObject *JsonTranslator::ToJson(RenderTarget *rt)
{
    if(!rt) return nullptr;
    QJsonObject* ob = new QJsonObject();
    ob->insert("path",rt->GetPath());
    ob->insert("layer",rt->GetLayer());
    ob->insert("posx",rt->GetPos().x());
    ob->insert("posy",rt->GetPos().y());
    ob->insert("sizeTimes",rt->sizeTimes);
    return ob;
}

QJsonObject *JsonTranslator::ToJson(BGM *bgm)
{
    if(!bgm) return nullptr;
    QJsonObject* ob = new QJsonObject();
    ob->insert("path",bgm->path);
    ob->insert("loop",bgm->loop);
    return ob;
}

QJsonObject *JsonTranslator::ToJson(Linker *linker)
{
    QJsonObject* ob = new QJsonObject();
    ob->insert("name",linker->name);
    ob->insert("branchId",linker->branchId);
    ob->insert("cycleId",linker->cycleId);
    ob->insert("branchOptionText",linker->branchOptionText);
    //conditions
    QJsonArray conditions;
    for(int i = 0;i < linker->conditions.count();i++)
    {
        Condition* c = linker->conditions.at(i);
        QJsonObject o;
        o.insert("name",c->name);
        o.insert("key",c->key);
        o.insert("symbol",c->symbol);
        //data
        QJsonObject d;
        d.insert("dataType",c->data.dataType);
        switch(c->data.dataType)
        {
        case INT_:
            d.insert("data",c->data.i);
            break;
        case BOOL_:
            d.insert("data",c->data.b);
            break;
        case FLOAT_:
            d.insert("data",c->data.f);
            break;
        case STRING_:
            d.insert("data",c->data.s);
            break;
        case NONE_:
            break;
        default:
            break;
        }

        o.insert("data",d);
        conditions.append(o);
    }
    //actions
    QJsonArray actions;
    for(int i = 0;i < linker->actions.count();i++)
    {
        Action* a = linker->actions.at(i);
        QJsonObject o;
        o.insert("key",a->key);
        o.insert("DATAACT",a->act);
        //data
        QJsonObject d;
        d.insert("dataType",a->data.dataType);
        switch(a->data.dataType)
        {
        case INT_:
            d.insert("data",a->data.i);
            break;
        case BOOL_:
            d.insert("data",a->data.b);
            break;
        case FLOAT_:
            d.insert("data",a->data.f);
            break;
        case STRING_:
            d.insert("data",a->data.s);
            break;
        case NONE_:
            break;
        default:
            break;
        }
        o.insert("data",d);
        actions.append(o);
    }

    ob->insert("conditions",conditions);
    ob->insert("actions",actions);
    return ob;
}

NarrationNode *JsonTranslator::NodeFromJson(QJsonObject *ob)
{
    NarrationNode* node = new NarrationNode();
    QJsonObject tempob = (*ob)["bgm"].toObject();
    BGM* bgm = SoundFromJson(&tempob);
    node->bgm = bgm;
    tempob = (*ob)["bgse"].toObject();
    BGM* bgse = SoundFromJson(&tempob);
    node->bgse = bgse;
    node->stopBgm = (*ob)["bgmStop"].toBool();
    node->stopBgse = (*ob)["bgseStop"].toBool();
    node->soundPath = (*ob)["soundPath"].toString();
    node->name = (*ob)["name"].toString();
    node->text = (*ob)["text"].toString();
    node->id = (*ob)["id"].toInt();
    node->nodeName = (*ob)["nodeName"].toString();
    node->childId = (*ob)["childId"].toInt();
    node->parentId = (*ob)["parentId"].toInt();
    node->blackTime = (*ob)["blackTime"].toInt();
    node->black = (*ob)["black"].toBool();
    //imgs(RenderTargets
    QJsonArray imgs = (*ob)["imgs"].toArray();
    for(int i = 0;i < imgs.count(); i++)
    {
        QJsonObject ob = imgs[i].toObject();
        node->image.push_back(RenderTargetFromJson(&ob));
    }
    //linkers
    QJsonArray linkers = (*ob)["linkers"].toArray();
    for(int i = 0;i < linkers.count();i++)
    {
        QJsonObject ob = linkers[i].toObject();
        node->linkers.push_back(LinkerFromJson(&ob));
    }

    //effect
    node->effect = (*ob)["effect"].toBool();
    node->effectName = (*ob)["effectName"].toString();

    return node;
}

RenderTarget *JsonTranslator::RenderTargetFromJson(QJsonObject *ob)
{
    QString path = (*ob)["path"].toString();
    int layer = (*ob)["layer"].toInt();
    float posx = (*ob)["posx"].toDouble();
    QString s = (*ob)["posx"].toString();
    float posy = (*ob)["posy"].toDouble();
    float sizeTimes = (*ob)["sizeTimes"].toDouble();
    RenderTarget* r = new RenderTarget(path,QVector2D(posx,posy),layer,sizeTimes);
    return r;
}

BGM *JsonTranslator::SoundFromJson(QJsonObject *ob)
{
    BGM* sound = new BGM();
    //TODO
    sound->path = (*ob)["path"].toString();
    sound->loop = (*ob)["loop"].toBool();
    return sound;
}

Linker *JsonTranslator::LinkerFromJson(QJsonObject *ob)
{
    Linker* linker = new Linker();
    //TODO
    linker->name = (*ob)["name"].toString();
    linker->branchId = (*ob)["branchId"].toInt();
    linker->cycleId = (*ob)["cycleId"].toInt();
    linker->branchOptionText = (*ob)["branchOptionText"].toString();

    //consditions
    QJsonArray cs = (*ob)["conditions"].toArray();
    for(int i = 0;i < cs.count();i++)
    {
        QJsonObject ob = cs[i].toObject();
        Condition* c = new Condition();
        c->name = ob["name"].toString();
        c->key = ob["key"].toString();
        c->symbol = (SYMBOL)ob["symbol"].toInt();
        //DATA
        QJsonObject dOb = ob["data"].toObject();
        Data data;
        c->data = data;
        c->data.dataType = (DATATYPE)dOb["dataType"].toInt();
        switch(c->data.dataType)
        {
        case INT_:
            c->data.i = dOb["data"].toInt();
            break;
        case BOOL_:
            c->data.b = dOb["data"].toBool();
            break;
        case FLOAT_:
            c->data.f = dOb["data"].toString().toFloat();
            break;
        case STRING_:
            c->data.s = dOb["data"].toString();
            break;
        case NONE_:
            break;
        default:
            break;
        }
        linker->conditions.push_back(c);
    }
    //actions
    QJsonArray as = (*ob)["actions"].toArray();
    for(int i = 0;i < as.count();i++)
    {
        QJsonObject ob = as[i].toObject();
        //key
        QString key = ob["key"].toString();
        //data
        Data data;
        QJsonObject dOb = ob["data"].toObject();
        data.dataType = (DATATYPE)dOb["dataType"].toInt();
        switch(data.dataType)
        {
        case INT_:
            data.i = dOb["data"].toInt();
            break;
        case BOOL_:
            data.b = dOb["data"].toBool();
            break;
        case FLOAT_:
            data.f = dOb["data"].toString().toFloat();
            break;
        case STRING_:
            data.s = dOb["data"].toString();
            break;
        case NONE_:
            break;
        default:
            break;
        }
        //dataAct
        DATAACT act = (DATAACT)ob["DATAACT"].toInt();
        Action* a = new Action(key,data,act);
        linker->actions.push_back(a);
    }
    return linker;
}

QJsonObject *JsonTranslator::DataToJson()
{
    QJsonObject* ob = new QJsonObject();
    QMap<QString,int> &id = GameData::Instance().intData;
    QMap<QString,QString> &sd = GameData::Instance().stringData;
    QMap<QString,float> &fd = GameData::Instance().floatData;
    QMap<QString,bool> &bd = GameData::Instance().boolData;
    for (QMap<QString,int>::const_iterator it = id.constBegin(); it != id.constEnd(); it++)
    {
        ob->insert(it.key(),it.value());
    }
    for (QMap<QString,QString>::const_iterator it = sd.constBegin(); it != sd.constEnd(); it++)
    {
        ob->insert(it.key(),it.value());
    }
    for (QMap<QString,float>::const_iterator it = fd.constBegin(); it != fd.constEnd(); it++)
    {
        ob->insert(it.key(),it.value());
    }
    for (QMap<QString,bool>::const_iterator it = bd.constBegin(); it != bd.constEnd(); it++)
    {
        ob->insert(it.key(),it.value());
    }
    return ob;
}

QJsonObject *JsonTranslator::ToJson(MyWidget *widget)
{
    QJsonObject* ob = new QJsonObject();
    ob->insert("name",widget->name);
    ob->insert("bgPath",widget->bgPath);
    ob->insert("myLayout",widget->myLayout);
    //label
    QJsonArray labelsArray;
    QVector<MyLabel*> labels = widget->GetLabels();
    for(int i = 0;i < labels.count(); i++)
    {
        labelsArray.append(*ToJson(labels[i]));
    }
    ob->insert("labels",labelsArray);

    //button
    QJsonArray btnsArray;
    QVector<MyButton*> btns = widget->GetButtons();
    for(int i = 0;i < btns.count(); i++)
    {
        btnsArray.append(*ToJson(btns[i]));
    }
    ob->insert("btns",btnsArray);

    return ob;
}

QJsonObject *JsonTranslator::ToJson(MyButton *btn)
{
    //只需存储info的名字，不需要存整个info
    QJsonObject* ob = new QJsonObject();
    ob->insert("name",btn->info->name);
    ob->insert("id",btn->id);
    ob->insert("posx",btn->pos().x());
    ob->insert("posy",btn->pos().y());
    ob->insert("width",btn->width());
    ob->insert("height",btn->height());
    return ob;
}

QJsonObject *JsonTranslator::ToJson(MyLabel *label)
{
    //只需存储info的名字，不需要存整个info
    QJsonObject* ob = new QJsonObject();
    ob->insert("id",label->id);
    ob->insert("posx",label->pos().x());
    ob->insert("posy",label->pos().y());
    ob->insert("width",label->width());
    ob->insert("height",label->height());
    ob->insert("name",label->info->name);
    return ob;
}

QJsonObject *JsonTranslator::ToJson(LabelInfo *info)
{
    QJsonObject* ob = new QJsonObject();
    ob->insert("name",info->name);
    ob->insert("img",info->img);
    ob->insert("text",info->text);
    //颜色
    ob->insert("r",info->color.red());
    ob->insert("g",info->color.green());
    ob->insert("b",info->color.blue());
    return ob;
}

QJsonObject *JsonTranslator::ToJson(ButtonInfo *info)
{
    QJsonObject* ob = new QJsonObject();
    ob->insert("name",info->name);
    ob->insert("interactive",info->interactive);
    ob->insert("soundPath",info->soundPath);
    ob->insert("img",info->img);
    ob->insert("inImg",info->inImg);
    ob->insert("pressImg",info->pressImg);
    ob->insert("event",info->event);
    ob->insert("eventWidgetName",info->eventWidgetName);
    ob->insert("text",info->text);
    //字体
    ob->insert("fontFamliy",info->font.family());
    ob->insert("fontSize",info->font.pointSize());
    //颜色
    ob->insert("r",info->color.red());
    ob->insert("g",info->color.green());
    ob->insert("b",info->color.blue());
    return ob;
}

MyWidget *JsonTranslator::WidgetFromJson(QJsonObject *ob)
{
    if(!ob)
        return nullptr;
    MyWidget* w = new MyWidget();
    w->name = (*ob)["name"].toString();
    w->bgPath = (*ob)["bgPath"].toString();

    QString s = (*ob)["name"].toString();
    //复原btns
    QJsonArray btns = (*ob)["btns"].toArray();
    for(int i = 0;i < btns.count();i++)
    {
        QJsonObject ob = btns[i].toObject();
        MyButton* b = ButtonFromJson(&ob);
        w->PushButton(b);
    }
    //复原labels
    QJsonArray labels = (*ob)["labels"].toArray();
    for(int i = 0;i < labels.count();i++)
    {
        QJsonObject ob = labels[i].toObject();
        MyLabel* l = LabelFromJson(&ob);
        w->PushLabel(l);
    }
    //复原界面

    int b = 0, l = 0;
    QVector<MyButton*> myBtns = w->GetButtons();
    QVector<MyLabel*> myLabels = w->GetLabels();
    while(b < btns.count() && l < labels.count())
    {
        if(myBtns[b]->id < myLabels[l]->id)
        {
            myBtns[b]->setParent(w);
            myBtns[b]->move(btns[b].toObject()["posx"].toDouble(),
                    btns[b].toObject()["posy"].toDouble());
            myBtns[b]->resize(btns[b].toObject()["width"].toDouble(),
                    btns[b].toObject()["height"].toDouble());
            myBtns[b]->Refresh(myBtns[b]->info);
            b++;
        }
        else
        {
            myLabels[l]->setParent(w);
            myLabels[l]->move(labels[l].toObject()["posx"].toDouble(),
                    labels[l].toObject()["posy"].toDouble());
            myLabels[l]->resize(labels[l].toObject()["width"].toDouble(),
                    labels[l].toObject()["height"].toDouble());
            myLabels[l]->Refresh(myLabels[l]->info);
            l++;
        }
    }
    while(b < btns.count())
    {
        myBtns[b]->setParent(w);
        myBtns[b]->move(btns[b].toObject()["posx"].toInt(),
                btns[b].toObject()["posy"].toInt());
        myBtns[b]->resize(btns[b].toObject()["width"].toInt(),
                btns[b].toObject()["height"].toInt());
        myBtns[b]->Refresh(myBtns[b]->info);
        b++;
    }
    while(l < labels.count())
    {
        myLabels[l]->setParent(w);
        myLabels[l]->move(labels[l].toObject()["posx"].toInt(),
                labels[l].toObject()["posy"].toInt());
        myLabels[l]->resize(labels[l].toObject()["width"].toInt(),
                labels[l].toObject()["height"].toInt());
        myLabels[l]->Refresh(myLabels[l]->info);
        l++;
    }
    if(!w->bgPath.isEmpty())
        w->RefreshBg(w->bgPath);
    LAYOUT layout = (LAYOUT)(*ob)["myLayout"].toInt();
    w->SetLayout(layout);
    return w;
}

MyButton *JsonTranslator::ButtonFromJson(QJsonObject *ob)
{
    if(!ob)
        return nullptr;
    MyButton* b = new MyButton();
    b->id = (*ob)["id"].toInt();
    QString name = (*ob)["name"].toString();
    if(UILib::Instance().btns.contains(name))
        b->info = UILib::Instance().btns[name];
    else
        return nullptr;
    return b;
}

MyLabel *JsonTranslator::LabelFromJson(QJsonObject *ob)
{
    if(!ob)
        return nullptr;
    MyLabel* l = new MyLabel();
    l->id = (*ob)["id"].toInt();
    QString name = (*ob)["name"].toString();
    if(UILib::Instance().labels.contains(name))
        l->info = UILib::Instance().labels[name];
    else
        return nullptr;
    return l;
}

ButtonInfo *JsonTranslator::BtnInfoFromJson(QJsonObject *ob)
{
    if(!ob)
        return nullptr;
    ButtonInfo* info = new ButtonInfo();
    info->name = (*ob)["name"].toString();
    info->interactive = (*ob)["interactive"].toBool();
    info->soundPath = (*ob)["soundPath"].toString();
    info->img = (*ob)["img"].toString();
    info->inImg = (*ob)["inImg"].toString();
    info->pressImg = (*ob)["pressImg"].toString();
    info->event = (BUTTONEVENT)(*ob)["event"].toInt();
    info->eventWidgetName = (*ob)["eventWidgetName"].toString();
    info->text = (*ob)["text"].toString();
    info->font = QFont((*ob)["fontFamliy"].toString(),(*ob)["fontSize"].toInt());
    info->color.setRed((*ob)["r"].toInt());
    info->color.setGreen((*ob)["g"].toInt());
    info->color.setBlue((*ob)["b"].toInt());
    return info;
}

LabelInfo *JsonTranslator::LabelInfoFromJson(QJsonObject *ob)
{
    if(!ob)
        return nullptr;
    LabelInfo* info = new LabelInfo();
    info->name = (*ob)["name"].toString();
    info->img = (*ob)["img"].toString();
    info->text = (*ob)["text"].toString();
    info->color.setRed((*ob)["r"].toInt());
    info->color.setGreen((*ob)["g"].toInt());
    info->color.setBlue((*ob)["b"].toInt());
    return info;
}

QJsonObject *JsonTranslator::ToJson(ParticleSystemInfo *info)
{
    QJsonObject* ob = new QJsonObject();

    ob->insert("name",info->name);
    ob->insert("maxCount",info->maxCount);
    ob->insert("lockScale",info->lockScale);

    ob->insert("min_x0",info->min_x0);
    ob->insert("min_y0",info->min_y0);
    ob->insert("max_x0",info->max_x0);
    ob->insert("max_y0",info->max_y0);

    ob->insert("min_xv",info->min_xv);
    ob->insert("min_yv",info->min_yv);
    ob->insert("max_xv",info->max_xv);
    ob->insert("max_yv",info->max_yv);

    ob->insert("min_xa",info->min_xa);
    ob->insert("min_ya",info->min_ya);
    ob->insert("max_xa",info->max_xa);
    ob->insert("max_ya",info->max_ya);

    ob->insert("min_xsize",info->min_xsize);
    ob->insert("min_ysize",info->min_ysize);
    ob->insert("max_xsize",info->max_xsize);
    ob->insert("max_ysize",info->max_ysize);

    //纹理
    QJsonArray array;
    for(int i = 0;i < info->TextureCount();i++)
    {
        array.append(info->GetPath(i));
    }
    ob->insert("texPath",array);

    return ob;
}

ParticleSystemInfo *JsonTranslator::ParticleInfoFromJson(QJsonObject *ob)
{
    ParticleSystemInfo* info = new ParticleSystemInfo();

    float min_x0 = (*ob)["min_x0"].toDouble();
    float min_y0 = (*ob)["min_y0"].toDouble();
    float max_x0 = (*ob)["max_x0"].toDouble();
    float max_y0 = (*ob)["max_y0"].toDouble();

    float min_xv = (*ob)["min_xv"].toDouble();
    float min_yv = (*ob)["min_yv"].toDouble();
    float max_xv = (*ob)["max_xv"].toDouble();
    float max_yv = (*ob)["max_yv"].toDouble();

    float min_xa = (*ob)["min_xa"].toDouble();
    float min_ya = (*ob)["min_ya"].toDouble();
    float max_xa = (*ob)["max_xa"].toDouble();
    float max_ya = (*ob)["max_ya"].toDouble();

    float min_xsize = (*ob)["min_xsize"].toDouble();
    float min_ysize = (*ob)["min_ysize"].toDouble();
    float max_xsize = (*ob)["max_xsize"].toDouble();
    float max_ysize = (*ob)["max_ysize"].toDouble();

    info->name = (*ob)["name"].toString();
    info->lockScale = (*ob)["lockScale"].toBool();
    info->maxCount = (*ob)["maxCount"].toInt();

    bool success = info->SetARange(min_xa,min_ya,max_xa,max_ya);
    success &= info->SetVRange(min_xv,min_yv,max_xv,max_yv);
    success &= info->SetSizeRange(min_xsize,max_xsize,min_ysize,max_ysize);
    success &= info->SetInitPosRange(min_x0,min_y0,max_x0,max_y0);

    if(!success)
        return nullptr;
    //纹理
    QJsonArray paths = (*ob)["texPath"].toArray();
    for(int i = 0;i < paths.count();i++)
    {
        info->AddTexture(paths[i].toString());
    }
    return info;
}

QJsonObject *JsonTranslator::ToJson()
{
    QJsonObject* ob = new QJsonObject();
    //string
    QJsonObject stringOb;
    for(auto it = GameData::Instance().stringData.begin();it != GameData::Instance().stringData.end();it++)
    {
        stringOb.insert(it.key(),it.value());
    }
    ob->insert("stringData",stringOb);
    //int
    QJsonObject intOb;
    for(auto it = GameData::Instance().intData.begin();it != GameData::Instance().intData.end();it++)
    {
        intOb.insert(it.key(),it.value());
    }
    ob->insert("intData",intOb);
    //float
    QJsonObject floatOb;
    for(auto it = GameData::Instance().floatData.begin();it != GameData::Instance().floatData.end();it++)
    {
        floatOb.insert(it.key(),it.value());
    }
    ob->insert("floatData",floatOb);
    //bool
    QJsonObject boolOb;
    for(auto it = GameData::Instance().boolData.begin();it != GameData::Instance().boolData.end();it++)
    {
        boolOb.insert(it.key(),it.value());
    }
    ob->insert("boolData",boolOb);
    //visible
    QJsonObject vOb;
    for(auto it = GameData::Instance().visible.begin();it != GameData::Instance().visible.end();it++)
    {
        vOb.insert(it.key(),it.value());
    }
    ob->insert("visible",vOb);
    return ob;
}

void JsonTranslator::ReadData(QJsonObject *ob)
{
    if(!ob)
        return;
    QJsonObject stringOb = (*ob)["stringData"].toObject();
    QJsonObject intOb = (*ob)["intData"].toObject();
    QJsonObject floatOb = (*ob)["floatData"].toObject();
    QJsonObject boolOb = (*ob)["boolData"].toObject();
    QJsonObject vOb = (*ob)["visible"].toObject();

    //string
    QStringList keys = stringOb.keys();
    for(int i = 0;i < keys.count();i++)
    {
        QString key = keys[i];
        GameData::Instance().stringData.insert(key,stringOb[key].toString());
    }

    //int
    keys.clear();
    keys = intOb.keys();
    for(int i = 0;i < keys.count();i++)
    {
        QString key = keys[i];
        GameData::Instance().intData.insert(key,intOb[key].toInt());
    }

    //float
    keys.clear();
    keys = floatOb.keys();
    for(int i = 0;i < keys.count();i++)
    {
        QString key = keys[i];
        GameData::Instance().floatData.insert(key,floatOb[key].toDouble());
    }

    //bool
    keys.clear();
    keys = boolOb.keys();
    for(int i = 0;i < keys.count();i++)
    {
        QString key = keys[i];
        GameData::Instance().boolData.insert(key,boolOb[key].toBool());
    }

    //visible
    keys.clear();
    keys = vOb.keys();
    for(int i = 0;i < keys.count();i++)
    {
        QString key = keys[i];
        GameData::Instance().visible.insert(key,vOb[key].toBool());
    }
}

QJsonObject *JsonTranslator::ToJson(RecordItem *item)
{
    QJsonObject* ob = new QJsonObject();
    ob->insert("narrationId",item->narrationId);
    ob->insert("time",item->time);
    QJsonObject* albumOb = ToJson(item->album);
    if(albumOb)
        ob->insert("album",*albumOb);
    QJsonObject* dataOb = ToJson();
    if(dataOb)
        ob->insert("data",*dataOb);
    ob->insert("id",item->id);
    return ob;
}

RecordItem *JsonTranslator::RecordFromJson(QJsonObject *ob)
{
    RecordItem* ri = new RecordItem();
    ri->narrationId = (*ob)["narrationId"].toInt();
    ri->time = (*ob)["time"].toString();
    QJsonObject albumOb = (*ob)["album"].toObject();
    ri->album = StringVectorFromJson(&albumOb);
    QJsonObject dataOb = (*ob)["data"].toObject();
    ri->data = new QJsonObject(dataOb);
    ri->id = (*ob)["id"].toInt();
    return ri;
}

QJsonObject *JsonTranslator::ToJson(QVector<QString> v)
{
    QJsonObject* ob = new QJsonObject();

    QJsonArray a;
    for(int i = 0;i < v.count(); i++)
    {
        a.append(v[i]);
    }
    ob->insert("array",a);
    return ob;
}

QVector<QString> JsonTranslator::StringVectorFromJson(QJsonObject *ob)
{
    QVector<QString> v;
    if(!ob)
        return v;
    QJsonArray a = (*ob)["array"].toArray();
    for(int i = 0;i < a.count();i++)
    {
        v.push_back(a.at(i).toString());
    }
    return v;
}

QByteArray JsonTranslator::Json2Byte(QJsonObject *ob)
{
    QJsonDocument doc(*ob);
    QByteArray ba = doc.toJson();
    return ba;
}

QJsonObject JsonTranslator::Byte2Json(QByteArray *json)
{
    QJsonObject obj;
    QJsonDocument doc = QJsonDocument::fromJson(*json);
    if (!doc.isNull() && doc.isObject()) {
        obj = doc.object();
    }
    return obj;
}


void JsonTranslator::Write(QByteArray s)
{
    QFile file("D:/test/out.txt");
    file.open(QIODevice::ReadWrite | QIODevice::Truncate);
    file.write(s);
    file.close();
}
